package View;

import java.awt.Image;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import Model.Customer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;


public class LoginPanel extends JPanel {
	private RestUWantGUI parent;
	private JTextField email;
	private JTextField password;

	/**
	 * Create the panel.
	 */
	public LoginPanel(RestUWantGUI GUI) {
		parent = GUI;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{62, 87, 29, 30, 161, 0};
		gridBagLayout.rowHeights = new int[]{107, 25, 0, 25, 25, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblEmailAddress = new JLabel("Email address");
		GridBagConstraints gbc_lblEmailAddress = new GridBagConstraints();
		gbc_lblEmailAddress.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblEmailAddress.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmailAddress.gridx = 2;
		gbc_lblEmailAddress.gridy = 1;
		add(lblEmailAddress, gbc_lblEmailAddress);
		
		email = new JTextField();
		GridBagConstraints gbc_email = new GridBagConstraints();
		gbc_email.fill = GridBagConstraints.BOTH;
		gbc_email.insets = new Insets(0, 0, 5, 5);
		gbc_email.gridx = 3;
		gbc_email.gridy = 1;
		add(email, gbc_email);
		email.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 2;
		gbc_lblPassword.gridy = 2;
		add(lblPassword, gbc_lblPassword);
		
		password = new JPasswordField();
		GridBagConstraints gbc_password = new GridBagConstraints();
		gbc_password.fill = GridBagConstraints.BOTH;
		gbc_password.insets = new Insets(0, 0, 5, 5);
		gbc_password.gridx = 3;
		gbc_password.gridy = 2;
		add(password, gbc_password);
		password.setColumns(10);
		
		JButton login = new JButton("Login");
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Customer cust = check_credentials();
				if (cust != null) {
					try {
						parent.loginToMenu(cust);
						parent.updateUsername(cust.getName());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					JOptionPane.showMessageDialog(null, "Email/password combination could not be verified. Please try again or re"
													+ "gister a new account", "Login Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		GridBagConstraints gbc_login = new GridBagConstraints();
		gbc_login.anchor = GridBagConstraints.WEST;
		gbc_login.fill = GridBagConstraints.VERTICAL;
		gbc_login.insets = new Insets(0, 0, 0, 5);
		gbc_login.gridx = 2;
		gbc_login.gridy = 4;
		add(login, gbc_login);
		
		JButton register = new JButton("Register Account");
		register.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parent.toRegister();
			}
		});
		GridBagConstraints gbc_register = new GridBagConstraints();
		gbc_register.anchor = GridBagConstraints.WEST;
		gbc_register.insets = new Insets(0, 0, 0, 5);
		gbc_register.fill = GridBagConstraints.VERTICAL;
		gbc_register.gridx = 3;
		gbc_register.gridy = 4;
		add(register, gbc_register);

		password.addKeyListener( new KeyListener() {

			@Override
			public void keyPressed(KeyEvent E) {
				if (E.getKeyCode() == KeyEvent.VK_ENTER) {
					Customer cust = check_credentials();
					if (cust != null) {
						try {
							parent.loginToMenu(cust);
							parent.updateUsername(cust.getName());
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} else {
						JOptionPane.showMessageDialog(null, "Email/password combination could not be verified. Please try again or re"
														+ "gister a new account", "Login Error", JOptionPane.ERROR_MESSAGE);
					}
				}
				
			}

			@Override
			public void keyReleased(KeyEvent E) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent E) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	private Customer check_credentials() {
		Customer newCust = null; 
		try {
	        
			Connection conn = null;
			Properties props = new Properties();
			props.put("user", "_445team11");
			props.put("password", "TewEkic");
			conn = DriverManager.getConnection("jdbc:mysql://cssgate.insttech.washington.edu/", props);
		
			
			String query = "SELECT userID, name, addressStreet, addressCity, addressState, "
						    + "addressZIP, phone FROM _445team11.Customer WHERE userID ='"+email.getText()+"' AND "
						    + "password = '" + password.getText() + "'";
		    Statement state = conn.prepareStatement(query);
			ResultSet rs = state.executeQuery(query);
			if (rs.next()) {
				newCust = new Customer(rs);
				
			} 
			 
		} catch (SQLException e) {
			System.out.println("Could not connect to database");
			e.printStackTrace();
		}
		return newCust;
	}
}
